package org.fdy.common.core.units;

import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.fdy.common.core.enums.ResultCode;
import org.fdy.common.core.exception.BaseException;

/**
 * @author fangdy
 * @create 2023-10-22 11:23
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiAssert {
    /**
     * 条件必须为true
     *
     * @param resultCode   resultCode
     * @param condition 判断条件
     */
    public static void isTrue(ResultCode resultCode, boolean condition) {
        if (!condition) {
            failure(resultCode, "");
        }
    }
    /**
     * 条件必须为true
     *
     * @param resultCode   resultCode
     * @param condition 判断条件
     * @param errorMsg  错误描述
     */
    public static void isTrue(ResultCode resultCode, boolean condition, String errorMsg) {
        if (!condition) {
            failure(resultCode, errorMsg);
        }
    }


    /**
     * 失败结果
     *
     * @param resultCode  resultCode
     * @param errorMsg 错误描述 如果有值则覆盖原错误提示
     */
    public static void failure(ResultCode resultCode, String errorMsg) {
        if (StrUtil.isBlank(errorMsg)) {
            throw new BaseException(resultCode);
        }
        throw new BaseException(resultCode.getCode(), errorMsg);
    }
}
